package com.formiaomiao.demain.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.formiaomiao.demain.R;
import com.formiaomiao.demain.model.DiscussBean;
import com.formiaomiao.demain.util.DateUtils;
import com.formiaomiao.demain.util.StaticValues;
import com.formiaomiao.demain.util.UseGlideUtils;

import java.util.List;

public class DiscussAdapter extends RecyclerView.Adapter<DiscussAdapter.ViewHolder> {

    Context context;
    List<DiscussBean.DataBean.ListBean> list;

    private onDiscussListener discussListener;

    public interface onDiscussListener {
        void headnameListener(View v, int i);

        void deleteListener(View v, int i);

        void discussListener(View v, int i);

        void discussdaataListener(View v, int i);
    }

    public void onDiscussListener(onDiscussListener discussListener) {
        this.discussListener = discussListener;
    }

    public DiscussAdapter(Context context, List<DiscussBean.DataBean.ListBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(context).inflate(R.layout.discuss_item, viewGroup, false);
        ViewHolder holder = new ViewHolder(v);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int i) {
        if (list.get(i).getToUserBean() == null) {
            holder.ll_discuss.setVisibility(View.GONE);
        } else {
            holder.ll_discuss.setVisibility(View.VISIBLE);
            holder.tv_discuss.setText(list.get(i).getToUserBean().getNickName());
        }
        if (list.get(i).getFromid() == StaticValues.userid) {
            holder.tv_delete.setVisibility(View.VISIBLE);
        } else {
            holder.tv_delete.setVisibility(View.GONE);
        }
        UseGlideUtils.setGlide(context, list.get(i).getUserBean().getUserHead(), holder.iv_userhead);
        holder.tv_username.setText(list.get(i).getUserBean().getNickName());
        holder.tv_time.setText(DateUtils.timess(Long.parseLong(list.get(i).getDiscussdate())));
        holder.tv_data.setText(list.get(i).getDiscussdata());
        holder.iv_userhead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                discussListener.headnameListener(view, i);
            }
        });
        holder.tv_username.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                discussListener.headnameListener(view, i);
            }
        });
        holder.tv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                discussListener.deleteListener(view, i);
            }
        });
        holder.tv_discuss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                discussListener.discussListener(view, i);
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                discussListener.discussdaataListener(view, i);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_userhead;
        TextView tv_username, tv_time, tv_delete, tv_discuss, tv_data;
        LinearLayout ll_discuss;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_userhead = itemView.findViewById(R.id.iv_userhead);
            tv_username = itemView.findViewById(R.id.tv_username);
            tv_time = itemView.findViewById(R.id.tv_time);
            tv_delete = itemView.findViewById(R.id.tv_delete);
            tv_discuss = itemView.findViewById(R.id.tv_discuss);
            tv_data = itemView.findViewById(R.id.tv_data);
            ll_discuss = itemView.findViewById(R.id.ll_discuss);
        }
    }
}
